/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.excel.ExportExcel;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.PartyStatistic;
import com.fosung.frame.modules.life.entity.Month;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.life.service.OrganizeLifeService;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 党组织统计Controller
 * 
 * @author ygl
 * @version 2017-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/statisticParty")
public class statisticPartyController extends BaseController {

	@Autowired
	private OrganizeLifeService organizeLifeService;
	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;

	@RequiresPermissions("approve:statisticParty:view")
	@RequestMapping(value = { "list", "" })
	public String list(OrganizeLife organizeLife, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if (organizeLife.getZq() == null) {
			organizeLife.setZq("2017");
		}
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<OrganizeLife> page = organizeLifeService.findListByZq(new Page<OrganizeLife>(request, response),
				organizeLife);
		// 因为组织生活分类可能包括多个，这里先spilt再单独查询拼成字符串显示到前台list

		List<OrganizeLife> olList = page.getList();

		if (olList.size() > 0) {
			for (int i = 0; i < olList.size(); i++) {
				StringBuffer typeNames = new StringBuffer();
				OrganizeLife o = olList.get(i);
				if (o.getTypeids() != null && !o.getTypeids().equals("")) {
					String[] types = o.getTypeids().split(",");

					if (types != null && types.length > 0) {
						for (String s : types) {
							// 循环遍历数组中元素，添加到StringBuilder 对象中
							String t = organizeLifeTypeService.get(s).getTypename();
							typeNames.append(t + ",");
						}
					}
					if (typeNames.length() > 0) {
						// 调用字符串的deleteCharAt()方法,删除最后一个多余的逗号
						typeNames.deleteCharAt(typeNames.length() - 1);
					}

				}
				o.setTypeids(typeNames.toString());
			}
		}

		model.addAttribute("page", page);
		return "modules/approve/statisticPartyList";
	}

	@RequiresPermissions("approve:partyStatistic:view")
	@RequestMapping(value = "form")
	public String form(PartyStatistic planStatistic, Model model) {
		model.addAttribute("planStatistic", planStatistic);
		return "modules/approve/partyStatisticForm";
	}

	/**
	 * 每月活动开展情况
	 * 
	 * @param partyStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findListByMonth")
	@ResponseBody
	public Month findPartyPmList(OrganizeLife organizeLife, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());

		Month list = organizeLifeService.findListByMonth(organizeLife);
		return list;
	}

	// 导出EXCEL
	@RequestMapping(value = "export", method = RequestMethod.GET)
	public String exportFile(OrganizeLife organizeLife, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		String fileName = "";
		try {
			if (organizeLife.getZq() == null) {
				organizeLife.setZq("2017");
			}
			organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
			List<OrganizeLife> Excellist = organizeLifeService.findListByZq(organizeLife);
			if (Excellist.size() > 0) {
				for (int i = 0; i < Excellist.size(); i++) {
					StringBuffer typeNames = new StringBuffer();
					OrganizeLife o = Excellist.get(i);
					if (o.getTypeids() != null && !o.getTypeids().equals("")) {
						String[] types = o.getTypeids().split(",");

						if (types != null && types.length > 0) {
							for (String s : types) {
								// 循环遍历数组中元素，添加到StringBuilder 对象中
								String t = organizeLifeTypeService.get(s).getTypename();
								typeNames.append(t + ",");
							}
						}
						if (typeNames.length() > 0) {
							// 调用字符串的deleteCharAt()方法,删除最后一个多余的逗号
							typeNames.deleteCharAt(typeNames.length() - 1);
						}

					}
					o.setTypeids(typeNames.toString());

					switch (o.getJhstate()) {
					default:
						o.setJhstate("");
						break;
					case "1":
						o.setJhstate("未提交");
						break;
					case "2":
						o.setJhstate("待审核");
						break;
					case "3":
						o.setJhstate("同意");
						break;
					case "4":
						o.setJhstate("退回");
						break;
					case "5":
						o.setJhstate("拒绝");
						break;
					case "6":
						o.setJhstate("取消");
						break;
					case "7":
						o.setJhstate("结束");
						break;
					}
				}
			}

			if (fileName == "") {
				fileName = organizeLife.getZq() + "年度组织生活统计.xlsx";
			}

			new ExportExcel(fileName, OrganizeLife.class).setDataList(Excellist).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出组织生活统计失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/approve/statisticParty/list?repage";
	}
}